using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VXSND
    {
        public DataRow Convert2XSND(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryXSND(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KBXSMANDT"] = " ".PadRight(3 ); 
         dwReturn["KBXSWERKS"] = " ".PadRight(4 ); 
         dwReturn["KBXSTGINR"] = " ".PadRight(4 ); 
         dwReturn["KBXSCONID"] = " ".PadRight(2 ); 
         dwReturn["KBXSTGMON"] = " ".PadRight(2 ); 
         dwReturn["KBXSTGDAY"] = " ".PadRight(2 ); 
         dwReturn["KBXSTGLNR"] = " ".PadRight(0 ); 
         dwReturn["KBXSQUITT"] = " ".PadRight(1 ); 
         dwReturn["KBXSPRTFL"] = " ".PadRight(1 ); 
         dwReturn["KBXSTGTIM"] = " ".PadRight(4 ); 
         dwReturn["KBXSSNDRP"] = " ".PadRight(2 ); 
         dwReturn["KBXSTNONR"] = " ".PadRight(1 ); 
         dwReturn["KBXSSWERK"] = " ".PadRight(4 ); 
         dwReturn["KBXSSNONR"] = " ".PadRight(1 ); 
         dwReturn["KBXSTGFREE"] = " ".PadRight(8 ); 
         dwReturn["KBXSTNNAM"] = " ".PadRight(6 ); 
         dwReturn["KBXSSNNAM"] = " ".PadRight(6 ); 
         dwReturn["KBXSTAPPL"] = " ".PadRight(5 ); 
         dwReturn["KBXSSAPPL"] = " ".PadRight(5 ); 
         dwReturn["KBXSDAT01"] = " ".PadRight(1000 ); 
         dwReturn["KBXSDAT02"] = " ".PadRight(1000 ); 
         dwReturn["KBXSSTATU"] = " ".PadRight(2 ); 
         dwReturn["KBXSSTAMP"] = " ".PadRight(14 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapXSND(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveXSND(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryXSND(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapXSND(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertXSND(dwLocal, ITXDBVERSION);
            }
            UpdateXSND(dwLocal);
        }
        public void InsertXSND(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO XSND(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryXSND(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM XSND WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM XSND  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "XSND";
            Return = Result;
            return Return;
        }
        public void DeleteXSND(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM XSND   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateXSND(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE XSND SET "
                    +"KBXSMANDT= :KBXSMANDT,"
                    +"KBXSWERKS= :KBXSWERKS,"
                    +"KBXSTGINR= :KBXSTGINR,"
                    +"KBXSCONID= :KBXSCONID,"
                    +"KBXSTGMON= :KBXSTGMON,"
                    +"KBXSTGDAY= :KBXSTGDAY,"
                    +"KBXSTGLNR= :KBXSTGLNR,"
                    +"KBXSQUITT= :KBXSQUITT,"
                    +"KBXSPRTFL= :KBXSPRTFL,"
                    +"KBXSTGTIM= :KBXSTGTIM,"
                    +"KBXSSNDRP= :KBXSSNDRP,"
                    +"KBXSTNONR= :KBXSTNONR,"
                    +"KBXSSWERK= :KBXSSWERK,"
                    +"KBXSSNONR= :KBXSSNONR,"
                    +"KBXSTGFREE= :KBXSTGFREE,"
                    +"KBXSTNNAM= :KBXSTNNAM,"
                    +"KBXSSNNAM= :KBXSSNNAM,"
                    +"KBXSTAPPL= :KBXSTAPPL,"
                    +"KBXSSAPPL= :KBXSSAPPL,"
                    +"KBXSDAT01= :KBXSDAT01,"
                    +"KBXSDAT02= :KBXSDAT02,"
                    +"KBXSSTATU= :KBXSSTATU,"
                    +"KBXSSTAMP= :KBXSSTAMP,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KBXSMANDT", OracleType.NVarChar).Value = dwData["KBXSMANDT"].ToString(); 
          or1.Parameters.Add("KBXSWERKS", OracleType.NVarChar).Value = dwData["KBXSWERKS"].ToString(); 
          or1.Parameters.Add("KBXSTGINR", OracleType.NVarChar).Value = dwData["KBXSTGINR"].ToString(); 
          or1.Parameters.Add("KBXSCONID", OracleType.NVarChar).Value = dwData["KBXSCONID"].ToString(); 
          or1.Parameters.Add("KBXSTGMON", OracleType.NVarChar).Value = dwData["KBXSTGMON"].ToString(); 
          or1.Parameters.Add("KBXSTGDAY", OracleType.NVarChar).Value = dwData["KBXSTGDAY"].ToString(); 
          or1.Parameters.Add("KBXSTGLNR", OracleType.NVarChar).Value = dwData["KBXSTGLNR"].ToString(); 
          or1.Parameters.Add("KBXSQUITT", OracleType.NVarChar).Value = dwData["KBXSQUITT"].ToString(); 
          or1.Parameters.Add("KBXSPRTFL", OracleType.NVarChar).Value = dwData["KBXSPRTFL"].ToString(); 
          or1.Parameters.Add("KBXSTGTIM", OracleType.NVarChar).Value = dwData["KBXSTGTIM"].ToString(); 
          or1.Parameters.Add("KBXSSNDRP", OracleType.NVarChar).Value = dwData["KBXSSNDRP"].ToString(); 
          or1.Parameters.Add("KBXSTNONR", OracleType.NVarChar).Value = dwData["KBXSTNONR"].ToString(); 
          or1.Parameters.Add("KBXSSWERK", OracleType.NVarChar).Value = dwData["KBXSSWERK"].ToString(); 
          or1.Parameters.Add("KBXSSNONR", OracleType.NVarChar).Value = dwData["KBXSSNONR"].ToString(); 
          or1.Parameters.Add("KBXSTGFREE", OracleType.NVarChar).Value = dwData["KBXSTGFREE"].ToString(); 
          or1.Parameters.Add("KBXSTNNAM", OracleType.NVarChar).Value = dwData["KBXSTNNAM"].ToString(); 
          or1.Parameters.Add("KBXSSNNAM", OracleType.NVarChar).Value = dwData["KBXSSNNAM"].ToString(); 
          or1.Parameters.Add("KBXSTAPPL", OracleType.NVarChar).Value = dwData["KBXSTAPPL"].ToString(); 
          or1.Parameters.Add("KBXSSAPPL", OracleType.NVarChar).Value = dwData["KBXSSAPPL"].ToString(); 
          or1.Parameters.Add("KBXSDAT01", OracleType.NVarChar).Value = dwData["KBXSDAT01"].ToString(); 
          or1.Parameters.Add("KBXSDAT02", OracleType.NVarChar).Value = dwData["KBXSDAT02"].ToString(); 
          or1.Parameters.Add("KBXSSTATU", OracleType.NVarChar).Value = dwData["KBXSSTATU"].ToString(); 
          or1.Parameters.Add("KBXSSTAMP", OracleType.NVarChar).Value = dwData["KBXSSTAMP"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
